/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webanovare.bb;

import com.webanovare.model.GamesRegistry;
import com.webanovare.model.UserRegistry;
import com.webanovare.model.db.PlayedGame;
import com.webanovare.model.db.User;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 * Used on profile pages to fetch information about a user and its games played.
 * @author jarmju
 */
@ManagedBean
@ViewScoped
public class UserBB {
    
    private String username;
    private User user;
    @EJB
    GamesRegistry gr;
    @EJB
    UserRegistry ur;
    
    public String getUsername() {
        return this.username;
    }

    public void setUsername(String name) {
        this.username = name;
    }

    public User getUser() {
        return this.user;
    }
    
    public List<String> getGamesToString() {
        List<String> out = new ArrayList<>();
        List<PlayedGame> games = this.getGames();
        String game = "";
        String outcome;
        for (PlayedGame pg : games) {
            String opponent = pg.getOtherPlayer(username).getUsername();
            String score;
            if (pg.getWinner().getUsername().equals(username)) {
                outcome = "Won ";
                score = pg.getWinnerPoints() + " - " + pg.getLoserPoints();
            }
            else if (pg.getLoser().getUsername().equals(username)) {
                outcome = "Lost ";
                score = pg.getLoserPoints() + " - " + pg.getWinnerPoints();
            }
            else {
                outcome = "Draw ";
                score = pg.getLoserPoints() + " - " + pg.getLoserPoints();
            }
            game = outcome + "against " + opponent + " with " + score;
            out.add(game);
        }
        return out;
    }
    
    public List<Long> getGameIds() {
        List<Long> out = new ArrayList<>();
        List<PlayedGame> games = this.getGames();
        for (PlayedGame pg : games) {
            Long id = pg.getId();
            out.add(id);
        }
        return out;
    }
    
    private List<PlayedGame> getGames() {
        List<PlayedGame> games = gr.getByUser(username);
        return games;
    }    
}
